const express = require('express')
const cors = require('cors')
const controller = require('./controller')
const tokenMiddleware = require('./middleware/tokenMiddleware')

// let obj = {a:1,b:2} 原始对象（字面量对象）

const app = express()

app.use(cors())

// 中间件：统一校验token。任何请求路径只要是/api打头,都会经过下面的中间件
app.use('/api', tokenMiddleware)

// 解析表单请求体 application/json
app.use(express.json())
// 解析表单请求体 application/x-www-form-urlencoded，extended: true 允许包含任何数据类型
app.use(express.urlencoded({ extended: true }))

app.post('/login', controller.login)

// 刷新token
app.post('/refreshToken', controller.refreshToken)

app.get('/api/order', controller.order)

app.get('/api/address', controller.address)

app.listen(4000, () => {
  console.log('server is running at port 4000')
})
